package cn.cdm.service;

import cn.cdm.dao.GoodsDao;
import cn.cdm.domain.MiaoshaGoods;
import cn.cdm.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsService {
	
	@Autowired
	GoodsDao goodsDao;
	
	public List<GoodsVo> listGoodsVo(){
		return goodsDao.listGoodsVo();
	}

	public GoodsVo getGoodsVoByGoodsId(long goodsId) {
		return goodsDao.getGoodsVoByGoodsId(goodsId);
	}

	public void reduceStockOld(GoodsVo goods){
		MiaoshaGoods g = new MiaoshaGoods();
		g.setGoodsId(goods.getId());
		int re = goodsDao.reduceStock(g);

		//System.out.println("减库存：" + re);
	}

	/**
	 * 减库存是否成功
	 * 减库存成功，返回true；
	 * 减库存失败，则返回false。
	 * @param goods
	 * @return
	 */
	public boolean reduceStock(GoodsVo goods){
		MiaoshaGoods g = new MiaoshaGoods();
		g.setGoodsId(goods.getId());
		int ret = goodsDao.reduceStock(g);
		return ret > 0;
	}


	public void reSetStock(List<GoodsVo> goodsVoList) {
		for (GoodsVo goodsVo : goodsVoList) {
			MiaoshaGoods miaoshaGoods = new MiaoshaGoods();
			miaoshaGoods.setGoodsId(goodsVo.getId());
			miaoshaGoods.setStockCount(goodsVo.getStockCount());
			goodsDao.reSetStock(miaoshaGoods);
		}
	}

}
